//***********************************************************************************
// Main function with the global timer and initial parameters defined

// Author: Aravindh U095232L

//***********************************************************************************


// ------------------------------------------------------------------
// Global AnimFrame timer
// ------------------------------------------------------------------

window.requestAnimFrame = (function(){
    return window.requestAnimationFrame  ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame  ||
    window.msRequestAnimationFrame  ||
    function(/* function */ callback, /* DOMElement */ element){
        window.setTimeout(callback, 1000 / 60);
    };
})();

// ------------------------------------------------------------------
// Load the game when html body is loaded
// ------------------------------------------------------------------
function load(){

	initGlobal();
	update();

};

// -------------------------------------------------------------------
// Globals
// -------------------------------------------------------------------
function initGlobal(){

    NPCsSpritesImage = document.getElementById("sprites");

            // camera
            var camera_args = {
                sx : 0,
                sy : 0,
                w : 10,
                h : 10
            };
            camera.init(camera_args);

            // map
            grids = [];
            for (var i=0; i<10; i++) {
                var newRow = [];
                for (var j=0; j<10; j++) {
                    var grid = {
                        type : "armyGovt",
                        NPCs : {},
                        buildings : {}
                    };
                    newRow.push(grid);
                }
                grids.push(newRow);
            }
            grids[0][0].NPCs = {
                Player : [
                {
                    x : 0,
                    y : 4,
                    type : "Rebel"
                }
                ]
            };
            var map_args = {
                row : 10,
                col : 10,
                grids : grids
            };
            map.init(map_args);
            player.setWalkDuration(300);
            dialogues = player.dialogueList;
             // rendering engine
             var rendering_args = {
                NPC_canvas : "NPC_canvas",
                background_canvas : "background_canvas",
                debug_map_canvas : "debug_map_canvas",
                map : map,
                player : player
            };

            renderingEngine.init(rendering_args);

            // input manager
            var input_args = {
                canvas : "NPC_canvas",
                player : player,
                camera : camera
            };
            inputManager.init(input_args);


    // Right Debug
    text = document.getElementById("text");
    txt = text.getContext("2d");
    dialog = document.getElementById("dialog");
    dlg = dialog.getContext("2d");
    debug_level = document.getElementById("debug_level");
    dbg = debug_level.getContext("2d");

    level_1 = [];
    level_temp = [];
    level_2 = [];
    level_3 = [];
    
    arrayOfSurroundingObjects = [];
    start_event = false;
    msg_queue = [];
    id_queue = [];
    msg = new message();
    conv_type = 0; 
    conv_object = 0 ;
    total_npc =1;
    mouse_x = 0;
    mouse_y= 0;
    mouse_down = false;
    global_timer = 0;

    //---------------------  Initial Global parameters
    
    army = (Math.round (Math.random() * (120 - 100) + 100) );;
    rebel = (Math.round (Math.random() * (120 - 100) + 100) );
    merchant = (Math.round (Math.random() * (10 - 9) + 9) );
    spy = (Math.round (Math.random() * (25 - 20) + 20) );
    citizen = 1000 - army - rebel - merchant - spy;
    for_war = 1;
    against_war = 1;
    total_people = army + rebel +merchant +spy +citizen;

    //Fights
    army_fight = 0;
    rebel_fight = 0;
    
    //Weapon transactions
    army_weapons = Math.round( Math.random()*(50)+(army-100) );
    rebel_weapons = 0;
    
    //TV station
    rebel_control = 0;
    army_control = 0;

    //Roadshow
    show_ratio = 0;
    
    //Personal influence
    rebel_influence = 0;
    spy_influence = 0;
    //***********************************************************************************
// Controller that decides which grid each level fallsin

// Author: Aravindh U095232L

//***********************************************************************************

    dialog.addEventListener('mousemove', ev_mousemove, false);
    dialog.addEventListener('mousedown', ev_mousedown, false);
    dialog.addEventListener('mouseup', ev_mouseup, false);


};

function output_global()
{
    console.log("Army: "+army+" Rebel: " + rebel +" Merchant: " + merchant+ " Spy: "+spy+" Citizen: "+ citizen + " For: "+for_war +" Against: "+against_war);
    console.log("Fight: "+army_fight+","+rebel_fight);
    console.log("Weapons: "+army_weapons +','+rebel_weapons);
    console.log("TV station: "+army_control+","+ rebel_control);
    console.log("Roadshow: "+show_ratio);
    console.log("Personal influence: "+spy_influence +','+rebel_influence);
}

// -------------------------------------------------------------------
// Get the mouse position relative to the canvas element.
// -------------------------------------------------------------------
function ev_mousemove(ev){ 
    if (ev.layerX || ev.layerX == 0) { 
        mouse_x = ev.layerX;
        mouse_y = ev.layerY;
    } 
    else if (ev.offsetX || ev.offsetX == 0) { // For Opera
        mouse_x = ev.offsetX;
        mouse_y = ev.offsetY;
    }
}   

function ev_mousedown(ev)
{
    mouse_down = true;
}

function ev_mouseup(ev)
{
    mouse_down = false;
}

// -------------------------------------------------------------------
// Main update thread 
// -------------------------------------------------------------------
function update(){

    inputManager.processEntry();

    if(conv_type==0)
        msg.control=0;
    controller();

    global_timer++; 
    animate();

};

// -------------------------------------------------------------------
// Rendering class
// -------------------------------------------------------------------
function animate(){

   renderingEngine.render();
   txt.clearRect(0,0,text.width,text.height);
   dbg.clearRect(0,0,debug_level.width,debug_level.height);
   dlg.clearRect(0,0,dialog.width,dialog.height);
    
    msg.displaytext();
   msg.display_list();

   msg.debug_text();

   requestAnimFrame(update);
}